//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package net.mingsoft.config;

import cn.hutool.core.codec.Base64;

import java.util.*;

import net.mingsoft.basic.realm.ManagerAuthRealm;
import net.mingsoft.basic.strategy.ILoginStrategy;
import net.mingsoft.basic.strategy.IModelStrategy;
import net.mingsoft.basic.strategy.ManagerLoginStrategy;
import net.mingsoft.basic.strategy.ManagerModelStrategy;
import net.mingsoft.config.filter.ChineseInvalidRequestFilter;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;

@Configuration
public class ShiroConfig {
    @Value("${ms.manager.path}")
    private String managerPath;
    @Value("${ms.shiro-key}")
    private String shiroKey;
    @Value("${ms.mock.enabled:false}")
    private boolean mockEnabled;
    @Value("${ms.statistics.summary.expose:false}")
    private boolean summaryIsExposed;

    public ShiroConfig() {
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator advisorAutoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        advisorAutoProxyCreator.setProxyTargetClass(true);
        return advisorAutoProxyCreator;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public AuthorizationAttributeSourceAdvisor getAuthorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator getDefaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator autoProxyCreator = new DefaultAdvisorAutoProxyCreator();
        autoProxyCreator.setProxyTargetClass(true);
        return autoProxyCreator;
    }

    @Bean
    public ShiroFilterFactoryBean shirFilter(SecurityManager securityManager, ChineseInvalidRequestFilter chineseInvalidRequestFilter) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();

        Map<String, Filter> filterMap = new HashMap<>();
        filterMap.put("invalid", chineseInvalidRequestFilter);
        shiroFilterFactoryBean.setFilters(filterMap);

        List<String> filterList = new ArrayList<>();
        filterList.add("invalid");
        shiroFilterFactoryBean.setGlobalFilters(filterList);

        shiroFilterFactoryBean.setSecurityManager(securityManager);
        shiroFilterFactoryBean.setLoginUrl(this.managerPath + "/login.do");
        shiroFilterFactoryBean.setUnauthorizedUrl(this.managerPath + "/404.do");
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap();
        filterChainDefinitionMap.put("/ms/**", "authc");
        filterChainDefinitionMap.put("/static/**", "anon");
        filterChainDefinitionMap.put("/html/**", "anon");
        filterChainDefinitionMap.put("/usb/**", "anon");
        filterChainDefinitionMap.put(this.managerPath + "/login.do", "anon");
        filterChainDefinitionMap.put(this.managerPath + "/checkLogin.do", "anon");

        if (summaryIsExposed) {
            filterChainDefinitionMap.put("/ms/cms/statistic/summary**", "anon");
        }

        /*if (!mockEnabled) {
            filterChainDefinitionMap.put(this.managerPath + "/**", "user");
        }*/

//        filterChainDefinitionMap.put("/ms/cms/document/preview**", "anon");

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilterFactoryBean;
    }

    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        securityManager.setRealm(this.customRealm());
        securityManager.setRememberMeManager(this.rememberMeManager());
        return securityManager;
    }

    public SimpleCookie rememberMeCookie() {
        SimpleCookie cookie = new SimpleCookie("rememberMe");
        cookie.setMaxAge(86400);
        return cookie;
    }

    public CookieRememberMeManager rememberMeManager() {
        CookieRememberMeManager cookieRememberMeManager = new CookieRememberMeManager();
        cookieRememberMeManager.setCookie(this.rememberMeCookie());
        cookieRememberMeManager.setCipherKey(Base64.decode(this.shiroKey));
        return cookieRememberMeManager;
    }

    @Bean
    public ManagerAuthRealm customRealm() {
        return new ManagerAuthRealm();
    }

    @Bean
    public IModelStrategy modelStrategy() {
        return new ManagerModelStrategy();
    }

    @Bean
    public ILoginStrategy loginStrategy() {
        return new ManagerLoginStrategy();
    }
}
